import { $C, $rp } from '@/utils/constants';
import formatCommentNote from './formatCommentNote';
import formatVoteNote from './formatVoteNote';
import formatOtherNote from './formatOtherNote';
import formatCtrbNote from './formatCtrbNote';
import formatAtNote from './formatAtNote';
import formatAnncNote from './formatAnncNote';

const noteT = $C.notifications.type;
const noteP = $C.notifications.position;

const formatSpaceInfo = (note) => {
  if (note.spaceId && note.spaceTitle) return { id: note.spaceId, title: note.spaceTitle };
  else if (note.position === noteP.groupAnnc) return {};
  return { err: true, msg: '消息空间space参数缺失' };
};

const formatGroupInfo = (note) => {
  if (note.position === noteP.groupAnnc) {
    if (note.groupId && note.groupTitle) return { id: note.groupId, title: note.groupTitle };
    return { err: true, msg: '消息群组group参数缺失' };
  } return {};
};

const formatNoteInfo = (note) => {
  const noteErrMsg = [];
  if (!note.action) noteErrMsg.push('消息动作action参数缺失');
  if (!note.position) noteErrMsg.push('消息位置position参数缺失');
  if (!note.position_parameter) noteErrMsg.push('消息位置position_parameter参数缺失');

  if (noteErrMsg.length) return { err: true, msg: noteErrMsg.join(', ') };

  const positionInfo = JSON.parse(note.position_parameter);
  return {
    action: note.action,
    position: note.position,
    id: positionInfo.id,
    title: positionInfo.title,
    parentPath: positionInfo.parentPath, // 当地点是目录下的资源时，会有父级的path

    // 当消息类型为点赞，并且时点赞评论时，因为positionInfo对position负责所以里面会有楼层信息
    floor: positionInfo.floor,
    content: positionInfo.content,
    parentId: positionInfo.parentId,
    parentContent: positionInfo.parentContent,
    parentFloor: positionInfo.parentFloor,

    // 申请公开空间
    spaceId: positionInfo.product_id,
    spaceTitle: positionInfo.product_title || '空间名缺失',
    reply: positionInfo.reply,
    state: positionInfo.state,

    // 社区邀请
    communityId: positionInfo.community_id,
    communityTitle: positionInfo.communityTitle,
  };
};

const formatAuthorInfo = (note) => {
  if (!note.author) return { err: true, msg: '消息发起人author参数缺失' };

  const authorInfo = JSON.parse(note.author);
  return {
    id: authorInfo.id, // api写法
    nickname: authorInfo.nickname, // api写法
    avatar: authorInfo.avatar, // api写法
    path: `/${$rp.USERS}/${authorInfo.id}`,
  };
};

const formatCommentInfo = (note) => {
  if (!note.comment) return { err: true, msg: '评论comment参数缺失' };

  const commentInfo = JSON.parse(note.comment);
  return {
    id: commentInfo.id,
    floor: commentInfo.floor,
    content: commentInfo.content,
    parentId: commentInfo.parentId,
    parentFloor: commentInfo.parentFloor,
    parentContent: commentInfo.parentContent,
  };
};

const formatVoteInfo = (note) => {
  if (!note.vote) return { err: true, msg: '点赞vote参数缺失' };

  const voteInfo = JSON.parse(note.vote);
  return {
    type: voteInfo.type,
    position: voteInfo.commentPosition,
    positionId: voteInfo.commentPositionId,
    positionTitle: voteInfo.commentPositionTitle,
    positionParentPath: voteInfo.commentPositionParentPath,
  };
};

// 将api传回的数据格式化成自己使用的样子
const formatToMine = (note) => {
  const spaceInfo = formatSpaceInfo(note); // 空间数据，id、title
  const groupInfo = formatGroupInfo(note);
  const noteInfo = formatNoteInfo(note); // 消息数据，地点position、动作action，当为目录下的资源时有更多参数
  const authorInfo = formatAuthorInfo(note); // 消息发起者数据，id、nickname、avatar
  const commentInfo = formatCommentInfo(note); // 评论数据，内容、楼层、父楼id、父楼楼层
  const voteInfo = formatVoteInfo(note); // 点赞数据

  return {
    id: note.id,
    type: note.type,
    read: note.read, // 已读？
    spaceInfo,
    groupInfo,
    noteInfo,
    authorInfo,
    commentInfo,
    voteInfo,
    created_at: note.created_at,
    updated_at: note.updated_at,
  };
};

export const formatNote = (note) => {
  const reNote = Object.assign(note.info || { err: true, msg: 'info参数缺失' }, {
    id: note.id,
    type: note.type,
    read: note.is_read,
    created_at: note.created_at,
    updated_at: note.updated_at,
  });
  if (reNote.err) return { id: note.id, err: true, msg: reNote.msg };
  const myNote = formatToMine(reNote);
  switch (note.type) {
    case noteT.at: return formatAtNote(myNote);
    case noteT.comments: return formatCommentNote(myNote);
    case noteT.votes: return formatVoteNote(myNote);
    case noteT.other: return formatOtherNote(myNote);
    case noteT.ctrb: return formatCtrbNote(myNote);
    case noteT.annc: return formatAnncNote(myNote);
    default: return { id: note.id, err: true, msg: '未识别到消息类型' };
  }
};

export const formatNotes = notes => notes.map(note => formatNote(note));
